package com.lzgcoding.onlineshopapp.feature.detail.component

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastForEach
import com.lzgcoding.onlineshopapp.ui.theme.Green
import com.lzgcoding.onlineshopapp.ui.theme.MediumPadding
import com.lzgcoding.onlineshopapp.ui.theme.SmallPadding
import com.lzgcoding.onlineshopapp.util.toColor

@Composable
fun ColorsAndSizeList(
    colors: List<String>,
    sizes: List<String>,
    selectedColor: String,
    selectedSize: String,
    onColorChange: (String) -> Unit,
    onSizeChange: (String) -> Unit
) {
    ListItem(
        overlineContent = {
            Text(
                text = "Colors & Size",
                style = MaterialTheme.typography.bodyLarge,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(bottom = MediumPadding)
            )
        },
        headlineContent = {
            Row(
                modifier = Modifier.fillMaxWidth(),
//                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(MediumPadding)
            ) {
                ColorList(colors, selectedColor, onColorChange)
                SizeList(sizes, selectedSize, onSizeChange)
            }
        }
    )
}

@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun RowScope.ColorList(
    colors: List<String>,
    selectedColor: String,
    onColorChange: (String) -> Unit
) {
    FlowRow(
        modifier = Modifier.weight(1f),
        horizontalArrangement = Arrangement.spacedBy(MediumPadding),
        verticalArrangement = Arrangement.spacedBy(SmallPadding)
    ) {
        colors.fastForEach {
            Box(
                modifier = Modifier
                    .clip(CircleShape)
                    .border(
                        width = 1.dp,
                        color = if (it == selectedColor) {
                            it.toColor()
                        } else {
                            Color.Transparent
                        },
                        shape = CircleShape
                    )
                    .background(color = MaterialTheme.colorScheme.background)
                    .padding(SmallPadding),
                contentAlignment = Alignment.Center
            ) {
                Box(
                    Modifier
                        .size(36.dp)
                        .clip(CircleShape)
                        .background(it.toColor())
                        .clickable {
                            onColorChange(it)
                        }
                )
            }
        }
    }
}

@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun RowScope.SizeList(
    sizes: List<String>,
    selectedSize: String,
    onSizeChange: (String) -> Unit
) {
    FlowRow(
        modifier = Modifier.weight(1f),
        horizontalArrangement = Arrangement.spacedBy(MediumPadding),
        verticalArrangement = Arrangement.spacedBy(SmallPadding)
    ) {
        sizes.fastForEach {
            Box(
                Modifier
                    .size(36.dp)
                    .clip(CircleShape)
                    .background(
                        color = if (selectedSize == it) {
                            Green
                        } else {
                            Color.Transparent
                        }
                    )
                    .border(1.dp, MaterialTheme.colorScheme.primaryContainer, CircleShape)
                    .clickable {
                        onSizeChange(it)
                    },
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = it,
                    style = MaterialTheme.typography.labelMedium
                )
            }
        }
    }
}